package controllers;

import models.SurveyResultsModel;
import mvc.IController;

public class SurveyResultsController implements IController
{

	public SurveyResultsController(SurveyResultsModel model)
	{
		itsSurveyResultsModel = model;
	}
	
	@Override
	public void onAction(String encoding)
	{
		String [] tokens = encoding.split("\\s");
		
		String command = tokens[0];
		if(tokens.length == 3 && command.equals("gradeQuestion"))
		{
			try
			{
				int position = Integer.parseInt(tokens[1]);
				double points = Double.parseDouble(tokens[2]);
				if(position < 1 || position > itsSurveyResultsModel.size())
				{
					System.out.println("Wrong question number");
					return;
				}
				itsSurveyResultsModel.gradeQuestion(position, points);
			}
			catch(NumberFormatException e)
			{
				System.out.println("Wrong grading command, try again");
			}
		}
		else if(tokens.length == 1 && command.equals("doneGrading"))
		{
			itsSurveyResultsModel.doneGrading();
		}
		else if(tokens.length == 1 && command.equals("?"))
		{
			System.out.println("Usage: (grading)");
			System.out.println("gradeQuestion [questionPosition] [score] : grade a question");
			System.out.println("doneGrading : finish grading and return to Desktop View");
			System.out.println("? : print this list");
		}
		else
		{
			System.out.println("Unknown command, type '?' for help");
		}
		
	}

	private SurveyResultsModel itsSurveyResultsModel;
}
